/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * @author ZhangMaoren 
 * @since 2015年6月15日
 * @version 0.1
 */
package com.suredy.resource.ctrl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.suredy.core.ctrl.BaseCtrl;
import com.suredy.core.mvc.model.Tree;
import com.suredy.resource.service.MenuSrv;
import com.suredy.security.entity.Role2PermissionEntity;
import com.suredy.security.entity.User2PermissionEntity;
import com.suredy.security.service.PermissionSrv;
import com.suredy.security.service.Role2PermissionSrv;
import com.suredy.security.service.User2PermissionSrv;


/**
 * @author ZhangMaoren
 *
 */
@Controller
@RequestMapping(value="/config")
public class ResourceCtrl extends BaseCtrl{
	
	@Autowired
	private MenuSrv menuSrv;
	
	@Autowired
	private Role2PermissionSrv role2permissionSrv;
	
	@Autowired
	private User2PermissionSrv user2permissionSrv;
	
	@Autowired
	private PermissionSrv permissionSrv;
	
	@RequestMapping("resource/user")
	@ResponseBody
	public Object ResourceUser(String userId) {
		List<User2PermissionEntity> userPermissions = this.user2permissionSrv.getByUser(userId);
		List<String> menuIds = new ArrayList<String>();
		for (User2PermissionEntity u2p : userPermissions) {
			menuIds.add(u2p.getPermission().getResource().getId());
		}
		
		Tree menuTree = new Tree();
		menuTree.setText("菜单类");
		menuTree.setCollapse(false);
		List<Tree> menuChildren = this.menuSrv.getMenuTree(null, menuIds);
		if (menuChildren != null) {
			menuTree.setChildren(new ArrayList<Tree>());
			menuTree.getChildren().addAll(menuChildren);
		}
		
		return menuTree;

	}
	
	@RequestMapping("resource/role")
	@ResponseBody
	public Object ResourceRole(String roleId) {
		List<Role2PermissionEntity> rolePermissions = this.role2permissionSrv.getByRole(roleId);
		List<String> menuIds = new ArrayList<String>();
		for (Role2PermissionEntity r2p : rolePermissions) {
			menuIds.add(r2p.getPermission().getResource().getId());
		}
		

		Tree menuTree = new Tree();
		menuTree.setText("菜单类");
		menuTree.setCollapse(false);
		List<Tree> menuChildren = this.menuSrv.getMenuTree(null, menuIds);
		if (menuChildren != null) {
			menuTree.setChildren(new ArrayList<Tree>());
			menuTree.getChildren().addAll(menuChildren);
		}

		return menuTree;

	}

	@RequestMapping("resource/tree")
	@ResponseBody
	public Object ResourceTree() {
		
		Tree menuTree = new Tree();
		menuTree.setText("菜单类");
		menuTree.setCollapse(false);
		List<Tree> menuChildren = this.menuSrv.getMenuTree(null);
		if (menuChildren != null) {
			menuTree.setChildren(new ArrayList<Tree>());
			menuTree.getChildren().addAll(menuChildren);
		}
		
		return menuTree;

	}
	
	
	@RequestMapping(value = "resource/view")
	public ModelAndView MenuManager() {
		ModelAndView view = new ModelAndView("/config/resource/resource-view");
		return view;
	}
	
}
